<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-home"></i>后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>属性列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
      <el-select v-model="templateId" placeholder="请选择属性模板" @change="loadAttributeList">
        <el-option
            v-for="item in attributeTemplateArr"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
<!--   id, template_id, name, description, type, input_type, value_list, unit, sort, is_allow_customize   -->
      <el-table-column prop="id" label="ID" align="center" width="50"></el-table-column>
      <el-table-column prop="templateId" label="属性模板ID" align="center" width="180"></el-table-column>
      <el-table-column prop="name" label="品牌名称" align="center" width="60"></el-table-column>
      <el-table-column prop="description" label="简介" align="center" width="180"></el-table-column>
      <el-table-column prop="type" label="属性类型" align="center" width="60"></el-table-column>
      <el-table-column prop="inputType" label="输入类型" align="center" width="60"></el-table-column>
      <el-table-column prop="valueList" label="备选值列表" align="center" width="140"></el-table-column>
      <el-table-column prop="unit" label="计量单位" align="center" width="60"></el-table-column>
      <el-table-column prop="sort" label="排序序号" align="center" width="60"></el-table-column>
      <el-table-column prop="isAllowCustomize" label="是否允许自定义" >
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.isAllowCustomize"
              @change="changeEnable(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="grey">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
            @click="handleEdit(scope.row)" ></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
            @click="openHandleDelete(scope.row)" ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "AttributeListView",
  data() {
    return {
      tableData: [],
      attributeTemplateArr: [],
      templateId:''
    }
  },
  created(){
    console.log("created...")
  },
  mounted() {
    console.log("mounted...")
    this.loadAttributeList()
    this.loadAttributeTemplateList()
  },
  methods: {
    changeEnable(attribute) {
      let url = "http://localhost:9080/attributes/" + attribute.id
      let tips = ["禁用","启用"]
      if(attribute.isAllowCustomize == 1){
        url += "/enable"
      }else {
        url += "/disable"
      }
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post(url).then(response=>{
        let responseBody = response.data
        console.log("responseBody=")
        console.log(responseBody)
        if(responseBody.state != 20000){

          let title = "操作失败"
          this.$alert(responseBody.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAttributeList()
            }
          })
        }else {
          this.$message.success(tips[attribute.enable] + "品牌成功")
        }
      })

    },
    tableRowClassName({row, rowIndex}) {
      if (rowIndex === 1) {
        return 'warning-row';
      } else if (rowIndex === 3) {
        return 'success-row';
      }
      return '';
    },
    loadAttributeList(){
      console.log("loadAttributeList...")
      let url = "http://localhost:9080/attributes?templateId=" + this.templateId;
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .get(url).then(response =>{
        console.log("attribute的response参数：",response)
        let responseBody = response.data;
        console.log("attribute的responseBody参数：",responseBody)
        this.tableData = responseBody.data
      })
    },
    loadAttributeTemplateList(){
      console.log("loadAttributeTemplateList...")
      let url = "http://localhost:9080/attribute-templates"
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .get(url).then(response =>{
        console.log("template的response参数：",response)
        let responseBody = response.data;
        console.log("template的responseBody参数：",responseBody)
        this.attributeTemplateArr = responseBody.data
      })
    },
    openHandleDelete(attribute) {
      this.$confirm('是否删除【'+attribute.name+'(id='+attribute.id+')】', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          this.handleDelete(attribute)
      }).catch(() => {
      });
    },
    handleDelete(attribute){
      // alert("你点击了【"+name+"(id="+id+")】的删除按钮")
      let url = 'http://localhost:9080/attributes/'+attribute.id+'/delete'
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post(url).then(response=>{
        let responseBody = response.data
        if(responseBody.state != 20000){
          this.$message.error("删除失败")
        }
        this.loadAttributeList()
      })
    },
    handleEdit(attribute){
      alert("你点击了【"+attribute.name+"(id="+attribute.id+")】的编辑按钮")
    }
  }
}
</script>

<style scoped>

</style>